package com.squid.templateProject.service.user;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.squid.templateProject.bean.entity.UserEntity;
import com.squid.templateProject.bean.enums.common.ActiveTokenEnums;
import com.squid.templateProject.bean.enums.common.DeleteTokenEnums;
import com.squid.templateProject.bean.enums.error.UserError1000Enums;
import com.squid.templateProject.bean.req.PageReq;
import com.squid.templateProject.bean.req.user.UserInsertUpdateReq;
import com.squid.templateProject.bean.req.user.UserSelectPageReq;
import com.squid.templateProject.bean.resp.PageResp;
import com.squid.templateProject.bean.resp.user.UserSelectPageResp;
import com.squid.templateProject.config.exception.ExceptionFactory;
import com.squid.templateProject.converter.user.UserConverter;
import com.squid.templateProject.mapper.user.UserEntityMapper;
import com.squid.templateProject.utils.LimitUtils;
import com.squid.templateProject.utils.TrimStringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * @author jinhang1
 * @description 针对表【user(用户表)】的数据库操作Service实现
 * @createDate 2024-01-23 17:08:02
 */
@Service
@Slf4j
public class UserEntityServiceImpl extends ServiceImpl<UserEntityMapper, UserEntity>
        implements UserEntityService {
    @Resource
    UserEntityMapper userEntityMapper;

    @Override
//    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public void insertUser(UserInsertUpdateReq req) {
        TrimStringUtils.trim(req);
        UserEntity userEntity = UserConverter.INSTANCE.to(req);
        checkRepeat(userEntity);
        try {
            this.save(userEntity);
        } catch (Exception e) {
            log.error("用户新增失败", e);
            throw ExceptionFactory.instanceBusinessException(UserError1000Enums.Code_1001);
        }
    }

    private void checkRepeat(UserEntity userEntity) {
        UserEntity dataUser = this.getOne(new LambdaQueryWrapper<UserEntity>()
                .eq(UserEntity::getUsername, userEntity.getUsername())
                .eq(UserEntity::getDeleteToken, DeleteTokenEnums.N.getCode())
                .eq(UserEntity::getActiveToken, ActiveTokenEnums.Y.getCode())
                .eq(UserEntity::getOrgId, userEntity.getOrgId())
                .ne(!ObjectUtils.isEmpty(userEntity.getId()), UserEntity::getId, userEntity.getId())
        );
        if (!ObjectUtils.isEmpty(dataUser)) {
            log.error("当前用户已经存在:{}", userEntity.getUsername());
            throw ExceptionFactory.instanceBusinessException(UserError1000Enums.Code_1000.getCode()
                    , StrUtil.format(UserError1000Enums.Code_1000.getMessage(), userEntity.getUsername()));
        }
    }

    @Override
    public void updateUser(UserInsertUpdateReq req) {
        TrimStringUtils.trim(req);
        UserEntity userEntity = UserConverter.INSTANCE.to(req);
        checkRepeat(userEntity);
        try {
            this.updateById(userEntity);
        } catch (Exception e) {
            log.error("用户更新失败", e);
            throw ExceptionFactory.instanceBusinessException(UserError1000Enums.Code_1002);
        }
    }

    @Override
    public void deleteUser(String id) {
        try {
            this.removeById(Long.valueOf(id));
        } catch (Exception e) {
            log.error("用户删除失败", e);
            throw ExceptionFactory.instanceBusinessException(UserError1000Enums.Code_1003);
        }
    }

    @Override
    public PageResp<UserSelectPageResp> pageUser(PageReq<UserSelectPageReq> req) {
        Page<UserSelectPageResp> page = new Page<>(req.getPageNum(), req.getPageSize());
        try {
            IPage<UserSelectPageResp> response = userEntityMapper.page(page, req.getRequest());
            return LimitUtils.formatResponse(response);
        } catch (Exception e) {
            log.error("用户查询失败", e);
            throw ExceptionFactory.instanceBusinessException(UserError1000Enums.Code_1004);
        }

    }
}




